<html>
<head>
    <meta charset="UTF-8">
    <title>Web Socket Test</title>
</head>
<body>

<div>
    <button type="button" id="login">登录</button>
    <p id="login-info"></p>
    <hr>
</div>

<div id="content">

</div>

<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
    var socket;
    function loginWs(address, pushId) {
        if (window.WebSocket) {
            socket = new WebSocket("ws://" + address + "?pushId=" + pushId);
            socket.onmessage = function (event) {
                $("#content").append('<p> Push :' + event.data + '</p>');
            };
            socket.onopen = function (event) {
                $("#content").append('<p> Login success at :' + address + '</p>');
            };
            socket.onclose = function (event) {
                console.log(event);
            };
        } else {
            alert("Your browser does not support Websockets. (Use Chrome)");
        }
    }
    function send(message) {
        if (!window.WebSocket) {
            return;
        }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(message);
        } else {
            alert("The socket is not open.");
        }
    }

    $("#login").click(function () {
        $.get("/auth", function (d) {
            $("#login-info").text(JSON.stringify(d));
            loginWs(d.address, d.pushId);
        });
    });
</script>
</body>
</html>